//
//  BHFunDetailMoreMarkTextCell.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/6/23.
//

import UIKit

class BHFunDetailMoreMarkTextCell: UITableViewCell {

    var inputTextEndCall: ((String?) -> Void)?
    var textViewTextLengthChange: ((Int) -> Void)?
    var maxCount = 200
    
    @IBOutlet weak var name_label: UILabel!
     
    @IBOutlet weak var text_view: UITextView!
    
    @IBOutlet weak var place_label: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.text_view.delegate = self
        self.place_label.addViewAction { [weak self] (label) in
            self?.text_view.becomeFirstResponder()
        }

    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
extension BHFunDetailMoreMarkTextCell : UITextViewDelegate {
    func textViewDidChange(_ textView: UITextView) {
        if textView.text.count > self.maxCount {
            textView.text = String(textView.text.prefix(self.maxCount))
        }
        self.place_label.isHidden = !textView.text.isEmpty
        self.textViewTextLengthChange?(textView.text.count)
    }
    
    
    func textViewDidEndEditing(_ textView: UITextView) {
        self.inputTextEndCall?(textView.text)
    }
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if text == "\n" {
            textView.resignFirstResponder()
        }
        return true
    }
}
